package com.gxc.string;

import java.util.*;

/**
 * 17. 电话号码的字母组合
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。
 * 答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 *
 * 2:abc
 * 3:def
 * 4:ghi
 * 5:jkl
 * 6:mno
 * 7:pqrs
 * 8:tuv
 * 9:wxyz
 */
public class LetterCombinations {

    public static void main(String[] args) {
        String digits = "23";
        System.out.println(handle(digits));
    }

    private static List<String> handle(String digits) {
        if (digits.isEmpty()) return new ArrayList<>();
        int n = digits.length();
        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        List<String> res = new ArrayList<>();
        backTrack(res, map, digits, 0, new StringBuilder());
        return res;
    }

    // 递归 回退
    private static void backTrack(List<String> res, Map<Character, String> map, String digits, int i, StringBuilder stringBuilder) {
        if (i == digits.length()) {
            //最后一位数字
            res.add(stringBuilder.toString());
            return;
        }
        String letters = map.get(digits.charAt(i));
        for (int j = 0; j < letters.length(); j++) {
            stringBuilder.append(letters.charAt(j));
            backTrack(res, map, digits, i+1, stringBuilder);
            //回退
            stringBuilder.deleteCharAt(i);
        }
    }

    class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> combinations = new ArrayList<String>();
            if (digits.length() == 0) {
                return combinations;
            }
            Map<Character, String> phoneMap = new HashMap<Character, String>() {{
                put('2', "abc");
                put('3', "def");
                put('4', "ghi");
                put('5', "jkl");
                put('6', "mno");
                put('7', "pqrs");
                put('8', "tuv");
                put('9', "wxyz");
            }};
            backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
            return combinations;
        }

        public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
            if (index == digits.length()) {
                combinations.add(combination.toString());
            } else {
                char digit = digits.charAt(index);
                String letters = phoneMap.get(digit);
                int lettersCount = letters.length();
                for (int i = 0; i < lettersCount; i++) {
                    combination.append(letters.charAt(i));
                    backtrack(combinations, phoneMap, digits, index + 1, combination);
                    combination.deleteCharAt(index);
                }
            }
        }
    }

    public class Solution17_1 {

        private String letterMap[] = {
                " ",    //0
                "",     //1
                "abc",  //2
                "def",  //3
                "ghi",  //4
                "jkl",  //5
                "mno",  //6
                "pqrs", //7
                "tuv",  //8
                "wxyz"  //9
        };

        private ArrayList<String> res;

        public List<String> letterCombinations(String digits) {

            res = new ArrayList<String>();
            if(digits.equals(""))
                return res;

            findCombination(digits, 0, "");
            return res;
        }

        private void findCombination(String digits, int index, String s){

            if(index == digits.length()){
                res.add(s);
                return;
            }

            Character c = digits.charAt(index);
            String letters = letterMap[c - '0'];
            for(int i = 0 ; i < letters.length() ; i ++){
                //字符相加移动至参数上，就不需要回滚
                findCombination(digits, index+1, s + letters.charAt(i));
            }

        }

    }


}

